import React, { useRef, useEffect } from 'react'
import JsBarcode from 'jsbarcode'
const Barcode = ({value, type, opt}, ref) => {
	const barEleRef = useRef(null)
	let barEle
	if (type === 'svg') {
		barEle = <svg ref={barEleRef} />
	} else if (type === 'canvas') {
		barEle = <canvas ref={barEleRef} />
	} else {
		barEle = <img ref={barEleRef} alt='' />
	}
	useEffect(() => {
		const option = {
			format: 'CODE128',
			width: 1,
			height: 30,
			displayValue: false,
			margin: 0,
			...opt
		}
		JsBarcode(barEleRef.current, value, option)
	}, [value, opt])
	return barEle
}
export default Barcode